layui.use(['table', 'admin', 'ax', 'func','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var form = layui.form;

    /**
     * 管理
     */
    var BtyBank = {
        tableId: "btyBankTable"
    };

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        tiger: 'click',
        // max: Feng.currentDate()
    });

/**
* 检索店名
    */

        var dmAjax = new $ax(Feng.ctxPath + "/btyBank/getDm");
        var dmResult = dmAjax.start();
        $.each(dmResult, function( index, val ) {
            $("#dm").append("<option value='"+val.yaoma+"'>"+val.dm+"</option>");
        } );
        form.render('select');

    function debounce(fn,wait){
        let timer = null;
        return function(){
            if(timer !== null){
                clearTimeout(timer);
            }
            timer = setTimeout(fn,wait);
        }
    }

    function searchByInput(val) {
        //获取input输入的值
        let dmAjax = new $ax(Feng.ctxPath + "/btyBank/getDm?dq="+val);
        let dmResult = dmAjax.start();
        $("#dm").empty();
        $("#dm").append("<option value=''>全部</option>");
        $.each(dmResult, function( index, val ) {
            $("#dm").append("<option value='"+val.yaoma+"'>"+val.dm+"</option>");
        } );
        form.render('select');
    }

    $("#dq").on("change",function(e){
        debounce(searchByInput(e.delegateTarget.value),500)
        //alert(e.delegateTarget.value);

    });

    /**
     * 初始化表格的列
     */
    BtyBank.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'id',hide:true,sort: true, title: ''},
            {field: 'dm', sort: true, title: '店名',width:250},
            {field: 'jysj', sort: true, title: '日期',width:120},
            {field: 'zc', sort: true, title: '支出',width:150},
            {field: 'sr', sort: true, title: '收入',width:150},
            {field: 'ye', sort: true, title: '余额',width:150},
            {field: 'zy', sort: true, title: '摘要',width:120},
            //{field: 'dfzh', sort: true, title: '贷方帐号'},
            {field: 'dfmc', sort: true, title: '户名',width:120},
            // {field: 'dfkhh', sort: true, title: '贷方开户行'},
            // {field: 'jyfs', sort: true, title: '交易方式'},
            //
            // {field: 'bz', sort: true, title: '备注'},
            {field: 'rq', sort: true, title: '时间'},
            //{field: 'yh', sort: true, title: '创建人'},

            //{align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyBank.search = function () {
        var queryData = {};

        queryData['jysj'] = $('#jysj').val();
        queryData['dm'] = $("#dm").find("option:selected").text();
        queryData['timeLimit'] = $("#timeLimit").val();

        table.reload(BtyBank.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyBank.openAddDlg = function () {
        func.open({
            title: '添加',
            content: Feng.ctxPath + '/btyBank/add',
            tableId: BtyBank.tableId
        });
    };


    BtyBank.confirmOk = function () {
        var timeLimit=$("#timeLimit").val();
        if(timeLimit==""){
           Feng.error("请选择日期！");
           return;
        }
        var dm=$("#dm").find("option:selected").text();
        var yaoma = $('#dm').val();
        if(dm==""){
            Feng.error("请选择店名！");
            return;
        }
        var region=$("#region").val();
        if(region==""){
            Feng.error("请选择大区！");
            return;
        }

            var ajax = new $ax(Feng.ctxPath + "/btyBank/confirmOk", function (data) {
                if(data.code==200){
                    Feng.success("凭证生成成功!");
                }else {
                    alert(data.message);
                }
                table.reload(BtyBank.tableId);
            }, function (data) {
                Feng.error("生成失败!" + data.responseJSON.message + "!");
            });
            ajax.set("timeLimit", timeLimit);
            ajax.set("dm", dm);
            ajax.set("yaoma", yaoma);
            ajax.set("region", region);
            ajax.start();
    };

    BtyBank.hykzc = function (){
        var timeLimit=$("#timeLimit").val();
        if(timeLimit==""){
            Feng.error("请选择日期！");
            return;
        }
        var dm=$("#dm").find("option:selected").text();
        var yaoma = $('#dm').val();
        if(dm==""){
            Feng.error("请选择店名！");
            return;
        }
        var region=$("#region").val();
        if(region==""){
            Feng.error("请选择大区！");
            return;
        }

        var ajax = new $ax(Feng.ctxPath + "/btyBank/hykzc", function (data) {
            if(data.code==200){
                Feng.success("凭证生成成功!");
            }else {
                alert(data.message);
            }
            table.reload(BtyBank.tableId);
        }, function (data) {
            Feng.error("生成失败!" + data.responseJSON.message + "!");
        });
        ajax.set("timeLimit", timeLimit);
        ajax.set("dm", dm);
        ajax.set("yaoma", yaoma);
        ajax.set("region", region);
        ajax.start();
    };

    BtyBank.zcpz = function () {
        var timeLimit=$("#timeLimit").val();
        if(timeLimit==""){
            Feng.error("请选择日期！");
            return;
        }
        var dm=$("#dm").find("option:selected").text();
        var yaoma = $('#dm').val();
        if(dm==""){
            Feng.error("请选择店名！");
            return;
        }
        var region=$("#region").val();
        if(region==""){
            Feng.error("请选择大区！");
            return;
        }

        var ajax = new $ax(Feng.ctxPath + "/btyBank/zcpz", function (data) {
            if(data.code==200){
                Feng.success("凭证生成成功!");
            }else {
                alert(data.message);
            }
            table.reload(BtyBank.tableId);
        }, function (data) {
            Feng.error("生成失败!" + data.responseJSON.message + "!");
        });
        ajax.set("timeLimit", timeLimit);
        ajax.set("dm", dm);
        ajax.set("yaoma", yaoma);
        ajax.set("region", region);
        ajax.start();
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyBank.openEditDlg = function (data) {
          func.open({
              title: '修改',
              content: Feng.ctxPath + '/btyBank/edit?=' + data.id,
              tableId: BtyBank.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyBank.exportExcel = function () {
        var checkRows = table.checkStatus(BtyBank.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyBank.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyBank/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyBank.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyBank.tableId,
        url: Feng.ctxPath + '/btyBank/list',
        page: true,
        limit:20,
        height: "full-59",
        cellMinWidth: 100,
        cols: BtyBank.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyBank.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyBank.confirmOk();

    });

    $('#hykzc').click(function () {
        BtyBank.hykzc();

    });

    $('#zcpz').click(function () {
        BtyBank.zcpz();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyBank.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyBank.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyBank.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyBank.onDeleteItem(data);
        }
    });
});
